﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Editor
{
    public class EditorCursor
    {
        private int _HEIGHT = 32;
        private int _WIDTH = 32;
        private int _OFFSET = 0;
        public int OldX = 1;
        public int OldY = 1;
        private Pen _coloredPen = new Pen(Color.Red, 2.0f);
        private Graphics _cursorGraphicsControl;
        private Bitmap _backgroundImage;
        private Bitmap _cursorImage;
        private Graphics _renderGraphicsControl;
        private Point _cursorLocation = new Point();
        public Point _backgroundImageLocation = new Point(0, 0);
        private Rectangle _drawingBoundary;

        public int OFFSET
        {
            get { return _OFFSET; }
            set { _OFFSET = OFFSET; }
        }

        public int HEIGHT
        {
            get { return _HEIGHT; }
            set { _HEIGHT = value; }
        }

        public int WIDTH
        {
            get { return _WIDTH; }
            set { _WIDTH = value; }
        }

        public EditorCursor(ref Graphics cursor1GraphicsDevice, ref Bitmap BackgroundGraphics)
        {
            _renderGraphicsControl = cursor1GraphicsDevice;
            _cursorImage = new Bitmap(BackgroundGraphics.Width, BackgroundGraphics.Height);
             _cursorGraphicsControl = Graphics.FromImage(_cursorImage);
            _backgroundImage = BackgroundGraphics;
            _cursorGraphicsControl.Clear(Color.Transparent);
            _drawingBoundary = Rectangle.Truncate(_renderGraphicsControl.VisibleClipBounds);
            UpdatePosition(0, 0);
        }

        public void UpdatePosition(int X, int Y)
        {
            if (X != OldX || Y != OldY)
            {
                _cursorLocation.X = X * _WIDTH;
                _cursorLocation.Y = Y * _HEIGHT;
                OldX = X;
                OldY = Y;
                Draw();
            }
        }

        public void AssignNewBitmap(IDisposable image)
        {
            _backgroundImage = (Bitmap)image;
        }

        public void Draw()
        {
            _cursorGraphicsControl.Clear(Color.Transparent);
            _cursorGraphicsControl.DrawRectangle(_coloredPen,new Rectangle(_cursorLocation,new Size(HEIGHT,WIDTH)));
            _renderGraphicsControl.DrawImage(_backgroundImage, _drawingBoundary, new Rectangle(_backgroundImageLocation, _drawingBoundary.Size), GraphicsUnit.Pixel);
            _renderGraphicsControl.DrawImageUnscaled(_cursorImage, new Point(0, 0));
        }
    }
}
